//获取图片地址
function GetQueryString(name) {
  var search = document.location;
  //alert(search);
  var pattern = new RegExp("[?&]" + name + "=([^&]+)", "g");
  var matcher = pattern.exec(search);
  var items = null;
  if (null != matcher) {
    try {
      items = decodeURIComponent(decodeURIComponent(matcher[1]));
    } catch (e) {
      try {
        items = decodeURIComponent(matcher[1]);
      } catch (e) {
        items = matcher[1];
      }
    }
  }
  return items;
}
// 关闭浏览器就清空
let session = {
  set(key, value) {
    window.sessionStorage.setItem(key, JSON.stringify(value)); //设置指定key的数据（JSON格式）
  },
  get(key) {
    return JSON.parse(window.sessionStorage.getItem(key)); //获取指定key的数据
  },
  remove(key) {
    window.sessionStorage.removeItem(key); //删除指定key的数据
  },
  clear() {
    window.sessionStorage.clear(); //清空所有的存储数据
  }
};
// 长期
let local = {
  set(key, value) {
    window.localStorage.setItem(key, JSON.stringify(value)); //设置指定key的数据（JSON格式）
  },
  get(key) {
    return JSON.parse(window.localStorage.getItem(key)); //获取指定key的数据
  },
  remove(key) {
    window.localStorage.removeItem(key); //删除指定key的数据
  },
  clear() {
    window.localStorage.clear(); //清空所有的存储数据
  }
};
/**
 *  _this: 当前 this
 *  response: 返回的数据
 * successFn：成功回调
 * errorFn： 错误回调
 * showMsg：打印提示  默认为提示 =》 传 true 不显示
 * ***/
var httpSuccess = (_this, response, successFn, errorFn, showMsg) => {
  if (response.data.code) {
    if (!showMsg) {
      if (response.data.code >= 500) {
        _this.$message({
          message: response.data.message,
          type: "error"
        });
      } else {
        _this.$message({
          message: response.data.message,
          type: "warning"
        });
      }
    }
    if (errorFn && typeof errorFn === "function") {
      errorFn();
    }
  } else {
    if (!showMsg) {
      _this.$message({
        message: response.data.message,
        type: "success"
      });
    }
    if (successFn && typeof successFn === "function") {
      successFn();
    }
  }
};
// 表单错误提交
var formSubmit = (_this, formName, fn) => {
  _this.$refs[formName].validate(valid => {
    if (valid) {
      if (fn && typeof fn === "function") fn();
    } else {
      _this.$message({
        message: "有内容未填完无法提交！",
        type: "error"
      });
    }
  });
};

export default {
  GetQueryString,
  session,
  local,
  httpSuccess,
  formSubmit
  // imgurl
};
